package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ConstanceVariable;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.ModelCustomConfigDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author: liyang
 * @date: 2021/10/27 21:05
 */
@Slf4j
@Component
public class SaveExternalFormPropertiesProcessor implements CustomPropertiesProcessor {
    @Override
    public boolean support(JsonNode elementNode) {

        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            JsonNode jsonNode = properties.get(ConstanceVariable.FORM_SOURCE);
            return nonNull(jsonNode) && Objects.equals("externalForm",jsonNode.textValue());
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        String nodeCode = context.getNodeCode();
        JsonNode jsonNode = context.getElementNode();
        JsonNode modelProperties = jsonNode.get("properties");
        if (Objects.nonNull(modelProperties)){
            String disPlayStyle = null;
            if (Objects.nonNull(modelProperties.get("displayStyle"))) {
                disPlayStyle = modelProperties.get("displayStyle").textValue();
            }
            ModelCustomConfigDomain modelCustomConfigDomain = new ModelCustomConfigDomain();
            modelCustomConfigDomain.setDeploymentId(context.getDeployId());
            modelCustomConfigDomain.setType(ConstanceVariable.EXTERNAL_FORM_SHOW_STYLE);
            modelCustomConfigDomain.setNodeCode(nodeCode);
            modelCustomConfigDomain.setConfig(disPlayStyle);
            modelCustomConfigDomain.save();
        }


    }
}
